package logic

import (
	"context"
	"gitee.com/Cauchy_AQ/vxopen/model/mongodb"

	"gitee.com/Cauchy_AQ/vxopen/rpc/transform/internal/svc"
	"gitee.com/Cauchy_AQ/vxopen/rpc/transform/transform"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserAvatarLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserAvatarLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserAvatarLogic {
	return &UserAvatarLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserAvatarLogic) UserAvatar(in *transform.UserAvatarReq) (*transform.UserAvatarResp, error) {
	var err error
	data := &mongodb.User{
		Avatar: in.Avatar,
		Mobile: in.Mobile,
		OpenID: in.Openid,
	}
	mongoClient := mongodb.GetMongoInstance(l.svcCtx.Config.ModelConfig)
	if len(in.Openid) > 0 {
		_, err = mongoClient.UserCol.UpdateAvatarByOpenId(context.Background(), data)
	}
	if err != nil {
		return nil, err
	}
	if len(in.Mobile) > 0 {
		_, err = mongoClient.UserCol.UpdateAvatarByMobile(context.Background(), data)
	}
	if err != nil {
		return nil, err
	}

	return &transform.UserAvatarResp{}, nil
}
